package com.android.dopewars;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class DrugSell extends Activity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.drug_quantity_set);
        
        ((TextView)findViewById(R.id.quantity_drug_name)).setText(
        		Global.current_drugs_.elementAt(Global.buy_sell_drug_).name());
        ((TextView)findViewById(R.id.quantity_drug_price)).setText(
        		"$" + Integer.toString(
        				Global.current_drugs_.elementAt(Global.buy_sell_drug_).currentPrice()));
        max_quantity_ = Global.dealer_.drugQuantity(Global.current_drugs_.elementAt(Global.buy_sell_drug_).name());
        ((EditText)findViewById(R.id.drug_quantity)).setText(Integer.toString(max_quantity_));
        ((SeekBar)findViewById(R.id.drug_quantity_slide)).setMax(max_quantity_);
        ((SeekBar)findViewById(R.id.drug_quantity_slide)).setProgress(max_quantity_);
        ((SeekBar)findViewById(R.id.drug_quantity_slide)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromTouch) {
				((EditText)findViewById(R.id.drug_quantity)).setText(Integer.toString(progress));
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        ((Button)findViewById(R.id.submit_quantity)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		// check the validity of the entered text
        		int new_quantity = Integer.parseInt(
        				((EditText)findViewById(R.id.drug_quantity)).getText().toString());
        		if (new_quantity < 0) {
        			new_quantity = 0;
        		}
        		if (new_quantity > Global.dealer_.drugQuantity(Global.current_drugs_.elementAt(Global.buy_sell_drug_).name())) {
        			new_quantity = Global.dealer_.drugQuantity(Global.current_drugs_.elementAt(Global.buy_sell_drug_).name());
        		}
        		Global.dealer_.removeDrugsFromInventory(
        				Global.current_drugs_.elementAt(Global.buy_sell_drug_).name(), 
        				new_quantity);
        		Global.dealer_.setCash(Global.dealer_.cash() + Global.current_drugs_.elementAt(
        				Global.buy_sell_drug_).currentPrice() * new_quantity);
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
        	}
        });
        
        WindowManager.LayoutParams dialog_params = getWindow().getAttributes();
    	dialog_params.width = WindowManager.LayoutParams.FILL_PARENT;
    	getWindow().setAttributes(dialog_params);
    }
    
    int max_quantity_;
}
